package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangHang @latimer.top
 * @version 1.0.0
 * @description TODO
 * @date 2022/12/3 8:43
 */
@Service
public class ICartServiceImpl implements ICartService {
    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    CartItemConverter cartItemConverter;

    /*
     *@description TODO 查看购物车所有商品
     *@param [userId]
     *@return java.util.List<com.cskaoyan.shopping.dto.CartProductDto>
     *@date 2022/12/3 14:23
     *@author liyangyang @latimer.top
    */
    @Override
    public List<CartProductDto> getCartListById(Long userId) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(userId + "");
        ArrayList<CartProductDto> cartProductDtos = new ArrayList<>();
        Iterator<Map.Entry<Long, CartProductDto>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Long, CartProductDto> next = iterator.next();
            CartProductDto value = next.getValue();
            cartProductDtos.add(value);
        }
        return cartProductDtos;
    }

    /*
     *@description TODO 将商品添加到购物车
     *@param [request]
     *@return void
     *@date 2022/12/3 14:23
     *@author liyangyang @latimer.top
    */
    @Override
    public void addToCart(CartRequest request) {
        Long userId = request.getUserId();
        Long productId = request.getProductId();
        Integer productNum = request.getProductNum();

        RMap<Long, CartProductDto> map = redissonClient.getMap(userId + "");
        Item item = itemMapper.selectByPrimaryKey(productId);
        CartProductDto cartProductDto = cartItemConverter.item2Dto(item);
        cartProductDto.setChecked("true");
        cartProductDto.setProductNum(Long.valueOf(productNum));

        map.put(productId, cartProductDto);

    }

    /*
     *@description TODO 修改购物车商品状态
     *@param [request]
     *@return void
     *@date 2022/12/3 14:44
     *@author liyangyang @latimer.top
    */
    @Override
    public void updateCartNum(CartRequest request) {
        Long userId = request.getUserId();
        Long productId = request.getProductId();
        Integer productNum = request.getProductNum();
        String checked = request.getChecked();

        RMap<Long, CartProductDto> map = redissonClient.getMap(userId + "");
        CartProductDto cartProductDto = map.get(productId);
        cartProductDto.setProductNum(Long.valueOf(productNum));
        cartProductDto.setChecked(checked);
        map.put(productId,cartProductDto);

    }

    /**
     * @param request 请求参数，包括userId和checked
     * @return void
     * @description 全选或全不选商品
     * @date 2022/12/3 9:19
     * @author ZhangHang @latimer.top
     */
    @Override
    public void checkAllCartItem(CheckAllItemRequest request) {
        Long userId = request.getUserId();
        String checked = request.getChecked();
        Map<Long, CartProductDto> map = redissonClient.getMap(userId + "");
        Iterator<Map.Entry<Long, CartProductDto>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Long, CartProductDto> next = iterator.next();
            Long key = next.getKey();
            CartProductDto value = next.getValue();
            if (!checked.equals(value.getChecked())) {
                value.setChecked(checked);
                map.put(key, value);
            }
        }

    }

    /**
     * @param uid 用户ID, itemId 商品ID
     * @return void
     * @description 删除购物车中的单个商品
     * @date 2022/12/3 9:29
     * @author ZhangHang @latimer.top
     */
    @Override
    public void deleteCartItem(Long uid, Long itemId) {
        Map<Long, CartProductDto> map = redissonClient.getMap(uid + "");
        Iterator<Map.Entry<Long, CartProductDto>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Long, CartProductDto> next = iterator.next();
            if (itemId.equals(next.getKey())) {
                iterator.remove();
            }
        }
    }

    /**
     * @param userId 用户ID
     * @return void
     * @description 删除所有选中的购物车商品
     * @date 2022/12/3 8:43
     * @author ZhangHang @latimer.top
     */
    @Override
    public void deleteCheckedItem(Long userId) {
        Map<Long, CartProductDto> map = redissonClient.getMap(userId + "");
        Iterator<Map.Entry<Long, CartProductDto>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Long, CartProductDto> entry = iterator.next();
            CartProductDto cartProductDto = entry.getValue();
            if ("true".equals(cartProductDto.getChecked())) {
                iterator.remove();
            }
        }
    }

}
